package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.mapper.ComplaintMapper;
import com.wjh.pojo.Complaint;
import com.wjh.pojo.ExpressageOrder;
import com.wjh.service.ComplaintService;
import com.wjh.service.LogisticsStaffService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Complaint)表服务实现类
 *
 * @author makejava
 * @since 2024-03-10 21:35:42
 */
@Service("complaintService")
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements ComplaintService {

    @Resource
    private LogisticsStaffService logisticsStaffService;

    @Override
    public ResponseResult getComplaintAll(Integer pageNum, Integer pageSize, String phone, String complaintName, Integer state) {
        LambdaQueryWrapper<Complaint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(phone!=null,Complaint::getPhone,phone);
        queryWrapper.like(complaintName!=null,Complaint::getComplaintName,complaintName);
        queryWrapper.eq(state != null,Complaint::getState,state);
        //分页查询所有
        Page<Complaint> complaintPage = new Page<>(pageNum,pageSize);
        Page<Complaint> page = page(complaintPage, queryWrapper);
        List<Complaint> records = page.getRecords();
        records.forEach(item -> {
            item.setCourierName(logisticsStaffService.getStaffById(item.getCourierId()).getName());
        });
        return ResponseResult.okResult(new PageVo(records,page.getTotal()));
    }

    @Override
    public ResponseResult updateComplaintState(Integer id, Integer state) {
        LambdaUpdateWrapper<Complaint> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(Complaint::getId,id);
        queryWrapper.set(Complaint::getState,state);
        update(queryWrapper);
        return ResponseResult.okResult();
    }
}
